
function setItem(name, item) {
    localStorage.setItem(name, JSON.stringify(item))
}

function getItem(name) {
    return JSON.parse(localStorage.getItem(name) || '[]')
}

export default {
    namespaced: true,
    state: {
        cart: getItem('cart'),
        buy: getItem('buy')
    },
    mutations: {
        addShopcart(state,goodsInfo){
            let flag = false
            state.cart.some(item=>{
                if(item.id === goodsInfo.id){
                    item.count += parseInt(goodsInfo.count)
                    flag = true
                    return true
                }
            })
            if(!flag){
                state.cart.push(goodsInfo)
            }
            setItem('cart',state.cart)
        }
    },
    actions: {},
    getters: {},
}
